var express = require("express");
var router = express.Router();
const {
  get,
  addCoupon,
  delCoupon,
  getOne,
  changeCoupon,
  searchCoupon,
} = require("../api/coupons");

//get请求获取优惠券数据和页面相关的操作
router.get("/", async function (req, res, next) {
  const { curPage, eachPage } = req.query;
  const data = await get({ curPage, eachPage });
  res.send(data);
});
//post请求新增优惠券/coupons
router.post("/", async function (req, res, next) {
  const { name, discount, minMoney, deadLine, navsId } = req.body;
  const data = await addCoupon({
    name,
    discount,
    minMoney,
    deadLine,
    navsId,
  });
  res.send(data);
});
//delete删除优惠券
router.delete("/del", async function (req, res, next) {
  const { _id } = req.body;
  const data = await delCoupon(_id);
  res.send(data);
});
//通过id来获取优惠券数据  /:_id等价于/coupons/:_id
router.get("/:_id", async function (req, res, next) {
  //路径的解析 /coupons/:_id
  const { _id } = req.query;
  const data = await getOne(_id);
  res.send(data);
});
//修改优惠券数据
router.put("/change", async function (req, res, next) {
  const newData = req.body;
  const data = await changeCoupon(newData);
  res.send(data);
});
//查询优惠券
router.get("/searchCou", async function (req, res, next) {
  const data = req.query;
  const feedBack = await searchCoupon(data);
  res.send(feedBack);
});
module.exports = router;
